﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hp.Vertica.Client.Internal;
using Vertica.Data.VerticaClient;

namespace Hp.Vertica.Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            btnControlTemplate.Click += (s1, e1) =>
            {
                TemplateViewer wView = new TemplateViewer();
                wView.Show();
            };
            btnSqlServerMigration.Click += (s1, e1) =>
            {
                SqlToVertica window = new SqlToVertica(ApplicationData.CurrentDatabase);
                window.Show();
            };

            btnNewConnection.Click += NewConnectionHandler;
            miNewConnection.Click += NewConnectionHandler;
        }

        private void NewConnectionHandler(object sender, RoutedEventArgs e)
        {
            VerticaConnection connectionWindow = new VerticaConnection(ApplicationData.CurrentDatabase);
            connectionWindow.Owner = this;
            connectionWindow.Closed += (sender2, eventArgs2) =>
            {
                VerticaDatabase context = (sender2 as VerticaConnection).DatabaseContext;
                if (context != null)
                {
                    ApplicationData.CurrentDatabase = context;
                    TreeViewItem item_database = (TreeViewItem)this.FindResource("nodeDatabase");
                    item_database.DataContext = context;
                    panelDatabases.Items.Add(item_database);
                }
            };
            connectionWindow.ShowDialog();
        }

        private void ImportFromSqlServer_Click(object sender, RoutedEventArgs e)
        {
            MenuItem source = sender as MenuItem;
            VerticaDatabase context = source.DataContext as VerticaDatabase;
            SqlToVertica window = new SqlToVertica(context.Clone());
            window.Show();
        }

        private void ViewSchemaDefinition_Click(object sender, RoutedEventArgs e)
        {
            Schema schema = (sender as MenuItem).DataContext as Schema;
            txtScriptPanel.Text += schema.CreateScript;
        }

        private void SelectTop1000_Click(object sender, RoutedEventArgs e)
        {
            Hp.Vertica.Client.Internal.Table tb = (sender as MenuItem).DataContext as Hp.Vertica.Client.Internal.Table;
            txtScriptPanel.Text += tb.SelectTop1000Script;
        }

        private void ViewTableDefinition_Click(object sender, RoutedEventArgs e)
        {
            Hp.Vertica.Client.Internal.Table tb = (sender as MenuItem).DataContext as Hp.Vertica.Client.Internal.Table;
            txtScriptPanel.Text += tb.CreateScript;

        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            Hp.Vertica.Client.Internal.Table tb = (sender as MenuItem).DataContext as Hp.Vertica.Client.Internal.Table;
            txtScriptPanel.Text += tb.SelectScript;

        }

        private void Insert_Click(object sender, RoutedEventArgs e)
        {
            Hp.Vertica.Client.Internal.Table tb = (sender as MenuItem).DataContext as Hp.Vertica.Client.Internal.Table;
            txtScriptPanel.Text += tb.InsertScript;

        }

        private void Update_Click(object sender, RoutedEventArgs e)
        {
            Hp.Vertica.Client.Internal.Table tb = (sender as MenuItem).DataContext as Hp.Vertica.Client.Internal.Table;
            txtScriptPanel.Text += tb.UpdateScript;

        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            Hp.Vertica.Client.Internal.Table tb = (sender as MenuItem).DataContext as Hp.Vertica.Client.Internal.Table;
            txtScriptPanel.Text += tb.DeleteScript;
        }
    }
}
